package servlet;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import bean.User;
import dao.UserDao;


/**
 * @author chailm
 * @version 创建时间：2017-4-14 上午11:21:53
 * 类说明
 */
public class LoginServlet extends HttpServlet{
	
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
//		String method=(String)request.getParameter("method");
//		System.out.println(method);
//		if(method.equals("login")){
			//获取一个参数
			String userName=(String)request.getParameter("userName");
			String password=(String)request.getParameter("password");
			//登录验证
			UserDao dao=new UserDao();
			User a=dao.findByName(userName);
			
			if(a==null){
				//账号不存在,转发会登陆页
				//目标：/netctoss/WEB-INF/main/login.jsp
				request.setAttribute("error","账号不存在");
				request.getRequestDispatcher("login.jsp").forward(request,response);
			}else if(!a.getPassWord().equals(password)){
				//密码错误，转发会登陆页
				request.setAttribute("error","密码错误");
				request.getRequestDispatcher("login.jsp").forward(request,response);
			}else{			
				//将账号存入Session,便于后续作登陆检查业务
				request.getSession().setAttribute("userName", userName);
				//成功,重定向到主页
				//当前：/netctoss/login.do
				//目标：/netctoss/toIndex.do
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		}
		
	//}	
}
